<template>
    <div class="page-wrapper">
        <div class="reset-container">
            <!-- 重置密码卡片 -->
            <div class="reset-card">
                <div class="card-header">
                    <h1 class="title">重置密码</h1>
                    <p class="subtitle">请填写信息以重置您的密码</p>
                </div>
                
                <van-form @submit="handleReset" class="reset-form">
                    <van-cell-group class="input-group">
                        <van-field 
                            autocomplete="current-username" 
                            v-model="resetForm.userAccount" 
                            label="用户名"
                            placeholder="请输入用户名" 
                            :rules="[
                                { required: true, message: '请填写用户名' },
                                { pattern: /^.{2,10}$/, message: '用户名在2到10字符之间' },
                            ]" 
                        />
                        <van-field 
                            autocomplete="new-password" 
                            v-model="resetForm.userPwd1" 
                            type="password" 
                            label="新密码"
                            placeholder="请输入新密码" 
                            :rules="[
                                { required: true, message: '请填写密码' },
                                {
                                    pattern: /^[a-zA-Z0-9]{6,}$/,
                                    message: '密码至少要6位，只能包含字母和数字'
                                }
                            ]" 
                        />
                        <van-field 
                            autocomplete="new-password" 
                            v-model="resetForm.userPwd2" 
                            type="password" 
                            label="确认密码" 
                            placeholder="请再次输入密码" 
                            :rules="[
                                { required: true, message: '请确认密码' },
                                {
                                    pattern: /^[a-zA-Z0-9]{6,}$/,
                                    message: '密码至少要6位，只能包含字母和数字'
                                },
                                { validator: validateConfirmPassword, message: '两次输入的密码不一致' }
                            ]" 
                        />
                    </van-cell-group>
                    
                    <div class="action-section">
                        <van-button 
                            round 
                            block 
                            type="primary" 
                            native-type="submit" 
                            class="reset-btn"
                            :loading="loading"
                        >
                            <span v-if="!loading">立即重置</span>
                            <span v-else>重置中...</span>
                        </van-button>
                        
                        <div class="auth-links">
                            <RouterLink to="/login" class="auth-link">
                                <van-icon name="arrow-left" />
                                <span>返回登录</span>
                            </RouterLink>
                            <RouterLink to="/register" class="auth-link">
                                <van-icon name="user-o" />
                                <span>注册账户</span>
                            </RouterLink>
                        </div>
                    </div>
                </van-form>
            </div>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref } from 'vue';  
import { useRouter } from 'vue-router';
import { showToast } from 'vant';
import { resetAccount } from '@/api';

const router = useRouter();
const loading = ref(false);

// 使用 reactive 而不是 ref
const resetForm = reactive({
    userAccount: "",
    userPwd1: "",
    userPwd2: ""
});

// 验证确认密码
const validateConfirmPassword = (value) => {
    return value === resetForm.userPwd1;
};

const handleReset = async () => {
    // 表单验证
    if (!resetForm.userAccount || resetForm.userAccount.trim() === '') {
        showToast('请输入用户名');
        return;
    }
    
    if (!resetForm.userPwd1) {
        showToast('请输入新密码');
        return;
    }
    
    if (!resetForm.userPwd2) {
        showToast('请确认密码');
        return;
    }
    
    if (resetForm.userPwd1 !== resetForm.userPwd2) {
        showToast('两次输入的密码不一致');
        return;
    }
    
    loading.value = true;
    
    try {
        // 根据 UserDto 调整字段名
        const submitData = {
            userAccount: resetForm.userAccount.trim(),  // 驼峰命名
            userPwd: resetForm.userPwd1                 // 驼峰命名
        };
        
        console.log('提交的数据:', submitData); // 调试日志
        
        await resetAccount(submitData);
        
        showToast({
            message: '密码重置成功',
            type: 'success'
        });
        
        setTimeout(() => {
            router.push('/login');
        }, 1500);
        
    } catch (error) {
        console.error('重置密码错误:', error);
        console.log('错误详情:', error.response?.data);
        showToast(error.response?.data?.message || error.message || '重置失败，请重试');
    } finally {
        loading.value = false;
    }
};
</script>

<style lang="scss" scoped>
.page-wrapper {
    min-height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    position: relative;
    
    &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: 
            radial-gradient(circle at 20% 80%, rgba(120, 119, 198, 0.3) 0%, transparent 50%),
            radial-gradient(circle at 80% 20%, rgba(255, 119, 198, 0.2) 0%, transparent 50%),
            radial-gradient(circle at 40% 40%, rgba(120, 219, 255, 0.2) 0%, transparent 50%);
    }
}

.reset-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    position: relative;
}

.reset-card {
    width: 100%;
    max-width: 440px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    border-radius: 24px;
    overflow: hidden;
    box-shadow: 
        0 20px 60px rgba(0, 0, 0, 0.15),
        0 0 0 1px rgba(255, 255, 255, 0.1);
    transform: translateY(0);
    transition: all 0.3s ease;
    
    &:hover {
        transform: translateY(-5px);
        box-shadow: 
            0 25px 80px rgba(0, 0, 0, 0.2),
            0 0 0 1px rgba(255, 255, 255, 0.15);
    }
    
    .card-header {
        padding: 40px 40px 20px;
        text-align: center;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        position: relative;
        overflow: hidden;
        
        &::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 1px, transparent 1px);
            background-size: 20px 20px;
            animation: float 6s ease-in-out infinite;
        }
        
        .title {
            font-size: 28px;
            font-weight: 700;
            margin: 0 0 8px 0;
            position: relative;
        }
        
        .subtitle {
            font-size: 14px;
            opacity: 0.9;
            margin: 0;
            font-weight: 400;
            position: relative;
        }
    }
}

.reset-form {
    padding: 40px;
    
    .input-group {
        background: transparent;
        margin-bottom: 30px;
        
        :deep(.van-field) {
            padding: 16px 0;
            margin-bottom: 20px;
            background: transparent;
            border-bottom: 1px solid #e8e8e8;
            transition: all 0.3s ease;
            
            &:last-child {
                margin-bottom: 0;
            }
            
            &:focus-within {
                border-bottom-color: #667eea;
                
                .van-field__label {
                    color: #667eea;
                }
            }
            
            .van-field__label {
                width: 80px;
                color: #333;
                font-weight: 600;
                font-size: 14px;
            }
            
            .van-field__control {
                font-size: 15px;
                color: #333;
                font-weight: 500;
                
                &::placeholder {
                    color: #999;
                    font-weight: 400;
                }
            }
            
            .van-field__error-message {
                font-size: 12px;
                margin-top: 4px;
            }
        }
    }
    
    .action-section {
        .reset-btn {
            height: 52px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            font-size: 16px;
            font-weight: 600;
            color: white;
            margin-bottom: 24px;
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            
            &::before {
                content: '';
                position: absolute;
                top: 0;
                left: -100%;
                width: 100%;
                height: 100%;
                background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
                transition: left 0.5s;
            }
            
            &:hover::before {
                left: 100%;
            }
            
            &:active {
                transform: translateY(1px);
                box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
            }
        }
        
        .auth-links {
            display: flex;
            justify-content: space-between;
            align-items: center;
            
            .auth-link {
                display: flex;
                align-items: center;
                gap: 6px;
                color: #666;
                font-size: 14px;
                text-decoration: none;
                padding: 8px 12px;
                border-radius: 8px;
                transition: all 0.3s ease;
                
                &:hover {
                    color: #667eea;
                    background: rgba(102, 126, 234, 0.1);
                    transform: translateY(-1px);
                }
                
                .van-icon {
                    font-size: 14px;
                }
                
                span {
                    font-weight: 500;
                }
            }
        }
    }
}

@keyframes float {
    0%, 100% {
        transform: translateY(0px) rotate(0deg);
    }
    50% {
        transform: translateY(-10px) rotate(180deg);
    }
}

// 响应式设计
@media (max-width: 480px) {
    .reset-container {
        padding: 15px;
    }
    
    .reset-card {
        .card-header {
            padding: 30px 20px 15px;
            
            .title {
                font-size: 24px;
            }
        }
    }
    
    .reset-form {
        padding: 30px 20px;
        
        .action-section {
            .auth-links {
                flex-direction: column;
                gap: 12px;
                
                .auth-link {
                    justify-content: center;
                    width: 100%;
                }
            }
        }
    }
}

@media (max-width: 360px) {
    .reset-form {
        padding: 20px 15px;
    }
}
</style>